package cate.game.pvp.arena;

import cate.game.framework.ModuleMan;
import cate.game.pvp.arena.core.ArenaRoleDetail;
import cate.game.pvp.arena.msg.*;
import cate.game.role.pvp.arena.msg.ArenaFightResultResp;
import cate.game.role.pvp.arena.msg.ArenaViewRankResp;
import cate.game.role.pvp.arena.msg.ArenaViewTopResp;
import cp.solution.rpc.RpcMsgHead;

public class ArenaManager extends ModuleMan {
    @Override
    public void initDependent() throws Exception {
        ArenaConstants.loadFromTable(game.table.common);
        game.rpcRecv().onFireAndForgetTemplate(ArenaInnerMsg.SEASON_CHANGE, Void.class, this::onSeasonChange);
    }

    /**
     * 赛季更新
     *
     * @param head 消息头
     * @param v 虚空
     */
    private void onSeasonChange(RpcMsgHead head, Void v) {
        game.role.activeForOnline(role -> {
            if (role.getPvp().arena.isOpen()) {
                role.getPvp().arena.reloadRankAndScore(false);
                role.getPvp().arena.refreshOpponents(true, true);
            }
        });
    }

    /**
     * 同步玩家布阵
     *
     * @param msg 布阵信息
     */
    public void syncRoleForm(AISyncRoleForm msg) {
        game.rpcSend().fireAndForgetCross(ArenaInnerMsg.SYNC_ROLE_FORM, msg);
    }

    /**
     * 获取玩家详细信息
     *
     * @param uid 角色UID
     */
    public ArenaRoleDetail getRoleDetail(String uid) {
        return game.rpcSend().requestResponseCross(ArenaInnerMsg.TRY_GET_DETAIL, uid, ArenaRoleDetail.class);
    }

    /**
     * 重新加载玩家排名和积分
     *
     * @param uid 角色UID
     */
    public AIReloadRankAndScore reloadRankAndScore(String uid) {
        return game.rpcSend().requestResponseCross(ArenaInnerMsg.RELOAD_ROLE_RANK, uid, AIReloadRankAndScore.class);
    }

    /**
     * 刷新对手列表
     *
     * @param uid 角色UID
     */
    public AIRefreshOpponents refreshOpponents(String uid) {
        return game.rpcSend().requestResponseCross(ArenaInnerMsg.REFRESH_OPPONENT, uid, AIRefreshOpponents.class);
    }

    /**
     * 上报战斗结果
     *
     * @param msg 消息
     */
    public ArenaFightResultResp reportFightResult(AIReportFightResult msg) {
        return game.rpcSend().requestResponseCross(ArenaInnerMsg.FIGHT_REPORT, msg, ArenaFightResultResp.class);
    }

    /**
     * 查看排行榜
     *
     * @param uid 角色UID
     */
    public ArenaViewRankResp getRankList(String uid) {
        return game.rpcSend().requestResponseCross(ArenaInnerMsg.GET_RANK_LIST, uid, ArenaViewRankResp.class);
    }

    /**
     * 查看大神榜
     */
    public ArenaViewTopResp getTopList() {
        return game.rpcSend().requestResponseCross(ArenaInnerMsg.GET_TOP_LIST, null, ArenaViewTopResp.class);
    }

    /**
     * 点赞大神
     *
     * @param uid 大神UID
     */
    public boolean likeTop(String uid) {
        return game.rpcSend().requestResponseCross(ArenaInnerMsg.LIKE_TOP, uid, Boolean.class);
    }
}
